package com.Webserver.core;

import com.Webserver.controller.UserController;
import com.Webserver.http.HttpRequest;
import com.Webserver.http.HttpResponse;
import java.io.File;
/**
 * 用于处理请求.
 */
public class DispatcherServlet {

    public void server(HttpRequest request, HttpResponse response){
        String path = request.getRequestURI();

        /**首先判断该请求是否为请求一个业务*/
        if("/myweb/regUser".equals(path)) {
            //处理注册
            UserController controller = new UserController();
            controller.reg(request, response);
        }else if("/myweb/loginUser".equals(path)){
            UserController controller = new UserController();
            controller.login(request, response);
        }else {
            File file = new File("./webapps" + path);
            /**如果请求的资源存在并且是一个文件则正确.*/
            if (file.exists() && file.isFile()) {
                response.setEntity(file);//调用响应对象,判断
            } else {
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                file = new File("./webapps/root/404.html");
                response.setEntity(file);
            }
        }
        //该响应头是告诉浏览器服务端是谁.
        response.putHeader("Server","WebServer");
    }
}
